/*
 * @Author: zhangfeng
 * @Date: 2022-09-06 16:02:42
 * @LastEditTime: 2023-12-27 18:41:37
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\server\user.server.js
 * © 2023 张峰，All rights reserved.
 */
const sequelize = require("sequelize");
const User = require("../model/user.model");
const Roleuser = require("../model/role-user.model");
const Role = require("../model/role.model");
const { create,getRolesByUserId, updateRolesByUserId } = require('./role-user.server')
class UserServer {
  // 创建用户
  async createUser(user_name, password) {
    // 写入数据库

    const result = await User.create({
      user_name,
      password,
    });
    return result.dataValues;
  }
  // 查询用户信息
  async getUserInfo({ id, user_name, password, is_admin }) {
    const whereOpt = {};

    id &&
      Object.assign(whereOpt, {
        id,
      });
    console.log;
    user_name &&
      Object.assign(whereOpt, {
        user_name,
      });

    password &&
      Object.assign(whereOpt, {
        password,
      });

    is_admin &&
      Object.assign(whereOpt, {
        is_admin,
      });

    const res = await User.findOne({
      attributes: ["id", "user_name", "password", "is_admin"],
      where: whereOpt,
    });
    console.log(res);
    console.log("res");
    return res ? res.dataValues : null;
  }
  //获取用户列表
  async getUserListAllServer() {
    const result = await User.findAll({
      attributes: {
        exclude: ["password", "zd_roles"],
        include: [
          [sequelize.literal("`zd_roles`.`id`"), "roleId"],
          [sequelize.literal("`zd_roles`.`name`"), "roleName"],
        ],
      },
      include: [
        {
          model: Role,
          as: "zd_roles",
          attributes: [],
        },
      ],
      // include: [
      //   {
      //     model: Role,
      //     attributes: ["id", "name"],
      //     through: { attributes: [] },
      //   },
      // ],
    });
    return result;
  }
  // 修改密码
  async updateById({ id, password, user_name, is_admin }) {
    const whereOpt = {
      id,
    };
    const newUser = {};

    user_name &&
      Object.assign(newUser, {
        user_name,
      });
    password &&
      Object.assign(newUser, {
        password,
      });
    is_admin &&
      Object.assign(newUser, {
        is_admin,
      });

    const res = await User.update(newUser, {
      where: whereOpt,
    });
    return res[0] > 0;
  }

  // 修改用户信息
  async updateUser(user){
    console.log(user)
    const {id,user_name,roleId} = user
    const newUser = {}
    let result;
    user_name && Object.assign(newUser, {user_name})
    const res = await User.update(newUser,{
      where: {
        id
      }
    })
    if(roleId) {
      const RolesByUserId = await  getRolesByUserId(id)
      if(RolesByUserId.length == 0) {
        result = await create({userId:id, roleId})
      } else {
        result = await updateRolesByUserId(id, roleId)
      }
    }
    return  res[0] > 0
  }
  // 删除用户信息
  async deleteUser({ id }) {
    //  删除当前用户角色关联信息
    const roleResult = await Roleuser.destroy({
      where: { userId: id },
    });
    // 删除用户信息
    const result = await User.destroy({
      where: { id },
    });
    return { result, roleResult };
  }
}

module.exports = new UserServer();
